#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"


static char usrdata[] = {"usr data"};

int main(int argc,char *argv[])
{
    char *filename;
    int fd,retval;
    char readbuf[100];
    char writebuf[100];
    if (argc != 3)
    {
        printf("error argc\r\n");
    }

    filename = argv[1];
    
    fd = open(filename,O_RDWR);
    if (fd < 0)
    {
        printf("Can`t open file:%s\r\n",filename);
        return -1;
    }

    if (atoi(argv[2]) == 1)
    {
        retval = read(fd,readbuf,50);

        if (retval < 0)
        {
            printf("read data failed\r\n");
        }
        else
        {
            printf("read data:%s\r\n",readbuf);
        }
    }

    if (atoi(argv[2]) == 2)
    {
        memcpy(writebuf,usrdata,sizeof(usrdata));

        retval = write(fd,writebuf,50);
        if (retval < 0)
        {
            printf("write data failed\r\n");
        }
        
    }
    
    retval = close(fd);
    if (retval < 0)
    {
        printf("close file failed\r\n");
    }

    return 0;
}

